package cn.hsw.platform.model;

import cn.hsw.platform.core.base.BaseConstant;
import cn.hsw.platform.core.base.BaseModel;
import com.alibaba.fastjson.annotation.JSONField;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.SQLDeleteAll;
import org.hibernate.annotations.Where;

import javax.persistence.*;
import java.io.Serializable;
import java.util.List;

/**
 * 角色实体
 *      每个角色可以有多个用户
 * @author 李子木
 */
@EqualsAndHashCode(callSuper = true)
@Data
@Entity
@Table(name = "hsw_manage_permission")
@Where(clause = BaseConstant.NOT_DELETE)
@SQLDelete(sql = "update hsw_manage_permission" + BaseConstant.DELETE_SQL)
@SQLDeleteAll(sql = "update hsw_manage_permission" + BaseConstant.DELETE_SQL)
public class PermissionModel extends BaseModel implements Serializable {


    private static final long serialVersionUID = -9170172212151307201L;

    /**
     * 父Id
     */
    @JSONField(serialize = false,name = "parent_pid")
    private Long parentPid;

    @OneToMany(fetch = FetchType.LAZY)
    @JoinColumn(name="parentPid")
    @JSONField(name = "sub_list")
    @OrderBy("sort asc")
    private List<PermissionModel> permissionModelList;

    /**
     * 名称
     */
    private String name;

    /**
     * 接口地址
     */
    private String url;

    /**
     * 权限code
     */
    private String code;

    /**
     * 类型 1、菜单 2、页面 3、接口
     */
    private int type;

    /**
     * 显示状态
     *  1 不显示
     *  -1 显示
     */
    private int state;

    /**
     * 排序
     */
    private Integer sort;

    private String target;

    private String icon;

    @Transient
    @JSONField(name = "parent_info")
    private ParentPermission parentInfo;

    @Transient
    public ParentPermission getParentPermission(Long pid, String name){
        return new ParentPermission(pid, name);
    }

    @Getter
    private static class ParentPermission{

        private Long pid;
        private String name;

        public ParentPermission(Long pid, String name) {
            this.pid = pid;
            this.name = name;
        }
    }
}
